<?php
    $base_path = $_SERVER['DOCUMENT_ROOT'] . '/nettbutikk';     //  endre /dev/nettbutikk til din path
    include_once $base_path . "/DB/db.php";
    include_once $base_path . "/class/userClass.php";


class ItemClass {

    // Get all information in DB about an item:
    function getAllItemData($itemId){
        global $db;
        $itemQuery = $db->query("
            SELECT *
            FROM item
            WHERE vid='$itemId'");
        return $itemQuery;
    }

    // Resizes an image and return its contents:
    function resizeImage($fName, $tName, $fSize, $fType){
        $fileName = $fName;
        $tmpName  = $tName;
        $fileSize = $fSize;
        $fileType = $fType;

        // Resize the image.
        $fileName = resizeImage($tmpName, $fileName, $fileType, $fileSize);

        // Get the image contents.
        $content = getContent($fileName);

        // unknown attribute..
        if(!get_magic_quotes_gpc())
            { $fileName = addslashes($fileName); }

        // Delete the temporary image-file.
        unlink($tmpName);
        unlink($fileName);
        return $content;
    }

    // Insert a new item in DB:
     function insertItemDb($kid, $name, $price, $description, $picture, $stock){
        global $db;
        // Database query:
        if($stock!=null){
            $query = "INSERT INTO item (kid, name, price, description, picture, stock)
            VALUES ($kid, '$name', $price, '$description', '$picture', $stock)";
        }else{
            $query = "INSERT INTO item (kid, name, price, description, picture)
            VALUES ($kid, '$name', $price, '$description', '$picture')";
        }

        $itemInsert = $db->prepare($query);
        $result = $itemInsert->execute();
    }

    // Update all attributes in an existing item in DB:
    function updateItemDbPicture($vid, $kid, $name, $price, $description, $picture, $stock, $deleted){
        global $db;
        if($stock!=null){   // if stock is set:
       $item_query = "
            UPDATE item
            SET name='$name', deleted=$deleted, description='$description',
                kid=$kid, price=$price, stock=$stock, picture='$picture'
            WHERE vid=$vid";
        }else{              // if stock is not set:
             $item_query = "
            UPDATE item
            SET name='$name', deleted=$deleted, description='$description',
                kid=$kid, price=$price, picture='$picture'
            WHERE vid=$vid";
        }
        $itemUpdate = $db->prepare($item_query);
        $itemUpdate->execute();
        header("Location: adminItem.php?alterItem=alterItem&itemId=$vid&updated=updated");
    }

    // Update all attributes, except picture, in an existing item in DB:
    function updateItemDb($vid, $kid, $name, $price, $description, $stock, $deleted){
        global $db;
        if($stock!=null){   // if stock is set:
       $item_query = "
            UPDATE item
            SET name='$name', deleted=$deleted, description='$description',
                kid=$kid, price=$price, stock=$stock
            WHERE vid=$vid";
        }else{              // if stock is not set:
            $item_query = "
            UPDATE item
            SET name='$name', deleted=$deleted, description='$description',
                kid=$kid, price=$price
            WHERE vid=$vid";
        }
        $itemUpdate = $db->prepare($item_query);
        $itemUpdate->execute();
        header("Location: adminItem.php?alterItem=alterItem&itemId=$vid&updated=updated");
    }

    // Get the name of an item:
    function getItemName($itemId){
        global $db;
        $item_query = $db->query("
            SELECT *
            FROM item
            WHERE vid = '$itemId'
        ");
        $row = $item_query->fetch();
        return $row['name'];
    }

    // Delete a spesific item:
     function deleteItem($itemId){
        global $db;
        $itemQuery="
        UPDATE item
        SET deleted=1
        WHERE vid='$itemId'";
        $itemDelete = $db->prepare($itemQuery);
        $result = $itemDelete->execute();
        return $result;
    }
}

$item = new ItemClass();
?>